using System;
using System.Xml;
using System.Collections;
using System.Runtime.InteropServices;

namespace Scenario.Modules.PCM.DataModel.ConfigurationManager.MetaWorkflow
{
	//*********************************************************************
	//
	// XMLAttributesRelationShips Class
	//
	// The XMLAttributesRelationShips class represents a collection 
	// base of XMLAttributesRelationShip
	// Its imports methods from WAPI.XmlObjectSet like get set and add
	// XMLAttributesRelationShips Has no fields
	//
	//*********************************************************************
	public class XMLAttributesRelationShips : bOS.XML.Data.XMLObjectSet 
	{
		public const string MAIN_NODE = "RelationShips";

		/** FIELDS **/

		/** no fields **/

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Empty **/
		public XMLAttributesRelationShips()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLAttributesRelationShips(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		/** INSTANCE METHODS **/

		override public void Init()
		{
			this.RemoveAll();
		}

		override public void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLAttributesRelationShip.MAIN_NODE:
						this.Add ( new XMLAttributesRelationShip(node));
						break;
				}
			}
		}

		
		override public bOS.XML.XMLNode GetXMLNode()
		{
			bOS.XML.XMLNode xmlNode = new bOS.XML.XMLNode(XMLAttributesRelationShips.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );
			}

			return xmlNode;
		}

		/** WAPI Wrappers to call SCENARIO ENGINE **/

		/** no wapi **/

		/** SORTING  FIELDS **/

		public enum SortFields 
		{
			Name
		}

		/** SORTING METHODS **/


		public void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.Name:
					InnerList.Sort(new NameComparer());
					break;
				
				
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class NameComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLAttributesRelationShip first = (XMLAttributesRelationShip) x;
				XMLAttributesRelationShip second = (XMLAttributesRelationShip) y;
				return first.Name.CompareTo(second.Name);
			}
		}

		
	}
}
